using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace XDL.Framework.Particles
{
    /// <summary>
    /// Emit a continuous line, using the <see cref="LineTrailSystem"/> particle system.
    /// </summary>
    public class LineTrailEmitter : 
        Emitter
    {
        LineTrailSystem m_lineSystem;
        Vector3 m_lineStartPosition;
        int m_vertexCount = 0;

        /// <summary>
        /// Creates a new emitter, using the given <see cref="LineSystem"/> instance.
        /// </summary>
        /// <param name="trail">Trail system</param>
        public LineTrailEmitter(LineTrailSystem lineSystem, float particlesPerSecond):
            base(particlesPerSecond)
        {
            m_lineSystem = lineSystem;
        }

        public override void Start(Vector3 position, Vector3 velocity)
        {
            base.Start(position, velocity);
            // reset internal counters;
            m_lineStartPosition = position;
            m_vertexCount = 1;
        }

        protected override void AddParticle(Vector3 position, Vector3 direction)
        {
            m_vertexCount++;
            // do we have an even number of points?
            if ((m_vertexCount % 2) == 0)
            {
                System.Diagnostics.Debug.Assert(m_vertexCount > 0);
                m_lineSystem.AddLine(m_lineStartPosition, position, direction);
                m_lineStartPosition = position;
            }
        }
    }
}
